package com.bankcomm.ui.specialwidget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bankcomm.R;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.ui.EMPView;
import com.bankcomm.util.LPUtils;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.LPToastUtil;

/**
 * 电影票座位。
 */
public class Seat extends Component {
	/**
	 * 用到的EMPView对象。
	 */
	private EMPView empview_;
	/**
	 * 座位上绘制的数字。
	 */
	public String id;
	/**
	 * 座位的seatNo属性。
	 */
	public String seatNo;
	/**
	 * 行。
	 */
	public int row;
	/**
	 * 列。
	 */
	public int col;
	/**
	 * 座位的状态。
	 */
	public int type;
	/**
	 * 座位按下时候的x坐标。
	 */
	private int xSeatDown_;
	/**
	 * 座位按下时候的y坐标。
	 */
	private int ySeatDown_;

	public class MySeat extends TextView implements CompositedComponent {

		public MySeat(Context context) {
			super(context);
			empview_ = (EMPView) context;
			// 座位的宽高。
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LPMovieTicket.seatImg_[type].getWidth(),
					LPMovieTicket.seatImg_[type].getHeight());
			setLayoutParams(lp);
		}

		@Override
		public Component composited() {
			return Seat.this;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			LPMovieTicket.paint_.setColor(0xFF000000);
			canvas.drawBitmap(LPMovieTicket.seatImg_[type], 0, 0,
					LPMovieTicket.paint_);
			canvas.drawText(
					id,
					((LPMovieTicket.seatImg_[type].getWidth() - LPMovieTicket.paint_
							.measureText(id)) / 2),
					((LPMovieTicket.seatImg_[type].getHeight() + LPMovieTicket.paint_
							.getTextSize()) / 2) - 2, LPMovieTicket.paint_);
		}

		@Override
		public boolean onTouchEvent(MotionEvent me) {
			// 取得触摸动作类型
			int action = me.getAction();
			switch (action) {
			// 按下操作
			case MotionEvent.ACTION_DOWN:
				xSeatDown_ = (int) me.getX();
				ySeatDown_ = (int) me.getY();
				this.requestFocus();
				break;
			case MotionEvent.ACTION_MOVE:
				break;
			case MotionEvent.ACTION_UP:
				int xUp = (int) me.getX();
				int yUp = (int) me.getY();
				// 移动位移8个像素以上算拖动,不响应弹起方法
				if (Math.abs(xUp - xSeatDown_) > 8
						|| Math.abs(yUp - ySeatDown_) > 8) {
					return super.onTouchEvent(me);
				}
				changeSeatStatus(Seat.this);
				break;
			default:
				return super.onTouchEvent(me);
			}
			return true;

		}

		void changeSeatStatus(Seat mySeat) {
			if (type == LPMovieTicket.CAN_BUY_IMG) { // 如果这个座位可以买。
				// 如果已经买了的座位超过了最大座位数，则提示不能继续购买。
				if (LPMovieTicket.selectedList.size() >= LPMovieTicket.canChooseMaxSeats) {
					LPToastUtil.getInstance().getToast(
							empview_.getResources().getString(
									R.string.movie_seatmax)
									+ LPMovieTicket.canChooseMaxSeats
									+ empview_.getResources().getString(
											R.string.movie_seats), empview_);

				}
				// 否则将座位状态改为已经购买，并且将当前作为加入已买座位列表。
				else {
					type = LPMovieTicket.SELECTED_IMG;
					LPMovieTicket.selectedList.add(mySeat);
				}
			}
			// 如果当前座位处于已经购买的状态，那么就将其变为待买的状态，并从已购列表中去除该座位。
			else if (type == LPMovieTicket.SELECTED_IMG) {
				type = LPMovieTicket.CAN_BUY_IMG;
				LPMovieTicket.selectedList.remove(mySeat);
			}
			// 拼接选座结果。
			String value = "";
			if (LPMovieTicket.selectedList != null
					&& LPMovieTicket.selectedList.size() != 0) {
				for (int j = 0; j < LPMovieTicket.selectedList.size(); j++) {
					value = value
							+ LPMovieTicket.selectedList.elementAt(j).seatNo
							+ "|";
				}
				value.substring(0, value.length() - 2);
			}
			// 赋值选座结果。
			LPMovieTicket.itself.attrValue_ = value;
			LPMovieTicket.itself.setPropertyByName("value",
					LPMovieTicket.itself.attrValue_);
			// 刷新当前座位。
			invalidate();
		}
	}

	@Override
	public void initRealView(Activity activity, String nodeValue) {
		super.initRealView(activity, nodeValue);
		realView_ = new MySeat(activity);
	}
	
	@Override
	public void releaseResource(Component component) {
		super.releaseResource(component);
	}
}
